#include "taskdialog.h"
#include "ui_taskdialog.h"
#include "categoryselectdialog.h"
#include "workflow.h"
#include <QtGui/QPushButton>

namespace acidone
{

TaskDialog::TaskDialog(Task &task, const WorkflowBase *workflow, Database::Subjects subjects, QWidget *parent)
	:QDialog(parent)
	,ui(new Ui::TaskDialog)
	,m_task(&task)
	,m_categoryManager(workflow->categoryManager())
{
	Q_ASSERT(m_task->subject() != 0);
	Q_ASSERT(!m_task->categories().empty());
	Q_ASSERT(m_task->priority() >= Task::Low && m_task->priority() <= Task::High);

	ui->setupUi(this);
	showMaximized();

	ui->captionEdit->setPlainText(m_task->caption());
	ui->notesEdit->setPlainText(m_task->notes());

	{
		ui->prioritySelector->addItem("Low");
		ui->prioritySelector->addItem("Normal");
		ui->prioritySelector->addItem("High");
		ui->prioritySelector->setCurrentIndex((int)m_task->priority());
	}
	{
		const bool beginDateTimeIsValid = !m_task->beginDateTime().isNull();
		const bool endDateTimeIsValid = !m_task->endDateTime().isNull();
		ui->beginDateCheck->setChecked(beginDateTimeIsValid);
		ui->endDateCheck->setChecked(endDateTimeIsValid);
		ui->beginDateTimeEdit->setEnabled(beginDateTimeIsValid);
		ui->endDateTimeEdit->setEnabled(endDateTimeIsValid);

		if (beginDateTimeIsValid)
			ui->beginDateTimeEdit->setDateTime(m_task->beginDateTime().toLocalTime());
		else
			ui->beginDateTimeEdit->setDateTime(QDateTime::currentDateTime());

		if (endDateTimeIsValid)
			ui->endDateTimeEdit->setDateTime(m_task->endDateTime().toLocalTime());
		else
			ui->endDateTimeEdit->setDateTime(QDateTime::currentDateTime());

		connect(ui->beginDateCheck, SIGNAL(clicked(bool)), ui->beginDateTimeEdit, SLOT(setEnabled(bool)));
		connect(ui->endDateCheck, SIGNAL(clicked(bool)), ui->endDateTimeEdit, SLOT(setEnabled(bool)));
	}

	{
		m_mainCategory = m_categoryManager->getTaskEmbeddedCategory(m_task);
		m_optionalCategories = m_categoryManager->getTaskOptionalCategories(m_task);

		Q_ASSERT(m_mainCategory != 0);

		const Database::Categories nextCategories = workflow->nextCategories(m_mainCategory);

		foreach(Category* category, nextCategories)
			ui->categorySelector->addItem(category->name(), qVariantFromValue(category));

		const int taskCategoryIdx = ui->categorySelector->findText(m_mainCategory->name());
		if (taskCategoryIdx != -1)
			ui->categorySelector->setCurrentIndex(taskCategoryIdx);
	}

	{
		foreach(Subject* subject, subjects)
			ui->subjectSelector->addItem(subject->name(), qVariantFromValue(subject));

		const int taskSubjectIdx = ui->subjectSelector->findText(m_task->subject()->name());
		if (taskSubjectIdx != -1)
			ui->subjectSelector->setCurrentIndex(taskSubjectIdx);
	}

	connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
	connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
	connect(ui->captionEdit, SIGNAL(textChanged()), this, SLOT(updateActions()));
	connect(ui->categorySelectorEx, SIGNAL(clicked()), this, SLOT(selectCategoryEx()));

	updateActions();
}

TaskDialog::~TaskDialog()
{
	delete ui;
}

void TaskDialog::accept()
{
	m_task->setCaption(ui->captionEdit->toPlainText());

	const int catIdx = ui->categorySelector->currentIndex();
	if (catIdx != -1)
	{
		Database::Categories categories = m_optionalCategories;
		categories.prepend(ui->categorySelector->itemData(catIdx).value<Category*>());
		m_task->setCategories(categories);
	}

	const int subjIdx = ui->subjectSelector->currentIndex();
	if (subjIdx != -1)
	{
		m_task->setSubject(ui->subjectSelector->itemData(subjIdx).value<Subject*>());
	}

	m_task->setPriority(Task::Priority(ui->prioritySelector->currentIndex()));

	if (ui->beginDateCheck->isChecked())
		m_task->setBeginDateTime(ui->beginDateTimeEdit->dateTime().toLocalTime());
	else
		m_task->setBeginDateTime(QDateTime());

	if (ui->endDateCheck->isChecked())
		m_task->setEndDateTime(ui->endDateTimeEdit->dateTime().toLocalTime());
	else
		m_task->setEndDateTime(QDateTime());

	m_task->setNotes(ui->notesEdit->toPlainText());

	QDialog::accept();
}

void TaskDialog::updateActions()
{
	bool canSave = !ui->captionEdit->toPlainText().isEmpty();
	ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(canSave);
}

void TaskDialog::selectCategoryEx()
{
	CategorySelectDialog dlg(m_categoryManager->optionalCategories(),
							 m_optionalCategories, this);

	if (dlg.exec() == QDialog::Accepted)
	{
		m_optionalCategories = dlg.selectedCategories();
	}
}

}
